# Author: HZT
# Date: 2023/10/20
import time

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import page
from config.config_loader import config
from page.web_ck_hk import WebCkHk
from page.web_sk_top import WebSkTop
from utils import UtilsDriver, login_with_session, GetLog, generate_random_string_sz, get_db_ye


class TestWebCkJk:
    def setup_class(self):
        self.log = GetLog.get_log()
        self.driver = UtilsDriver.get_web_driver()
        # 实例化
        self.ckHk = WebCkHk(self.driver)
        # 打开网页
        self.driver.get(config.URL)
        login_with_session('system1', self.driver)
        self.kh = generate_random_string_sz(5)
        self.yhm = '测服回款管理Test' + self.kh

    def teardown_class(self):
        # 在所有测试用例执行完毕后，关闭浏览器
        # UtilsDriver.get_web_driver().quit()
        pass

    # 新增回款成功
    def test01_ck_hk(self):
        try:
            self.ckHk.ck_hk_click_hkgl()
            self.ckHk.ck_hk_get_xz()
            db_ye = get_db_ye(config.CREDENTIALS["username_ck"])
            ym_ye = self.ckHk.ck_hk_get_ye().replace(',', '')
            assert db_ye == ym_ye
            self.ckHk.ck_hk_click_xz()
            hk_je = '1000'
            xm = '雷锋'
            filename = 'test002.png'
            bz = '学习雷锋好榜样！'
            self.ckHk.ck_hk_xz(self.driver, hk_je, self.yhm, xm, self.kh, filename, bz)
            # 等待页面回来首页
            db_ye2 = self.ckHk.ck_hk_get_ye()
            assert (float(db_ye) - float(hk_je)) == float(db_ye2.replace(',', ''))
        except Exception as e:
            self.log.error(f"新增回款异常，原因是{e}")
            self.ckHk.base_get_img()
            assert False

    # 充值
    def test02_ck_hk(self):
        try:
            driver_new = webdriver.Chrome()
            driver_new.maximize_window()
            driver_new.get(config.URL)
            skTop = WebSkTop(driver_new)
            # 登录
            login_with_session('system2', driver_new)
            skTop.sk_top_click_qcz()
            time.sleep(5)
            bz = "谢谢老铁"
            filename = "test002.png"
            je = "500"
            skTop.base_switch_window("收卡商充值")
            location = page.create_web_sk_top_cz_selector(self.yhm)
            WebDriverWait(driver_new, 60, 1).until(
                EC.presence_of_element_located(location))
            driver_new.execute_script("window.scrollBy(0, 500);")
            skTop.sk_top_cz(driver_new, self.yhm, je, filename, bz)
            assert "您已经打款" in skTop.sk_top_get_dkcg()
            driver_new.close()
        except Exception as e:
            self.log.error(f"充值异常，原因是{e}")
            self.ckHk.base_get_img()
            assert False

    # 回款待处理确认充值订单
    def test03_ck_hk(self):
        try:
            self.ckHk.ck_hk_click_hkdcl()
            self.ckHk.ck_hk_get_dkjt()
            skzh = self.ckHk.ck_hk_get_skzh()
            # czje = self.ckHk.ck_hk_get_czje()
            zt = self.ckHk.ck_hk_get_zt()
            assert self.yhm in skzh
            assert "证据已上传" == zt
            self.ckHk.ck_hk_click_cz()
            self.ckHk.ck_hk_click_qrwc()
            self.ckHk.ck_hk_click_qrtj()
            self.ckHk.ck_hk_click_tjok()
            zt = self.ckHk.ck_hk_get_zt()
            time.sleep(5)
            assert "成功" == zt
        except Exception as e:
            self.log.error(f"回款待处理确认异常，原因是{e}")
            self.ckHk.base_get_img()
            assert False
